package hk.edu.polyu.comp.motioncapture.application;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MotionCaptureGUI {

	private StyledText statusText;
	protected Shell shell;
	private Processor proc;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MotionCaptureGUI window = new MotionCaptureGUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();

		proc = new Processor();
		Thread thread = new Thread(proc);
		thread.start();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(final DisposeEvent arg0) {
				if (proc != null) {
					proc.done = true;
				}
			}
		});
		shell.setMinimumSize(new Point(400, 320));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		shell.setLayout(gridLayout);
		shell.setSize(400, 320);
		shell.setText("Motion Capture GUI");

		final Canvas mainCanvas = new Canvas(shell, SWT.BORDER);
		mainCanvas.addPaintListener(new PaintListener() {
			public void paintControl(final PaintEvent arg0) {
				GC gc = arg0.gc;

				Color blackColor = new Color(shell.getDisplay(), new RGB(0, 0,
						0));

				gc.setBackground(blackColor);
				gc.fillRectangle(0, 0, mainCanvas.getBounds().width, mainCanvas
						.getBounds().height);
			}
		});
		final GridData gd_mainCanvas = new GridData(SWT.FILL, SWT.FILL, true,
				true);
		gd_mainCanvas.heightHint = 225;
		gd_mainCanvas.widthHint = 277;
		mainCanvas.setLayoutData(gd_mainCanvas);

		final Composite commandComposite = new Composite(shell, SWT.NONE);
		final GridData gd_commandComposite = new GridData(SWT.FILL, SWT.FILL,
				false, false);
		gd_commandComposite.widthHint = 116;
		commandComposite.setLayoutData(gd_commandComposite);
		commandComposite.setLayout(new GridLayout());

		statusText = new StyledText(shell, SWT.V_SCROLL | SWT.READ_ONLY
				| SWT.H_SCROLL | SWT.BORDER);
		final GridData gd_statusText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_statusText.heightHint = 56;
		statusText.setLayoutData(gd_statusText);
		//
	}

	private class Processor implements Runnable {

		public boolean done = false;

		@Override
		public void run() {
			while (!done) {
				
			}

			System.out.println("End");
		}
	}

}
